#include"FinishGame.h"
#define fall_speed 0.5f
CFinish::CFinish()
{
	top=NULL;
	body=NULL;
	flag=NULL;
	home=NULL;
	list.pHead=list.pTail=NULL;
}
CFinish::~CFinish()
{
	if(top!=NULL) delete top;
	if(body!=NULL) delete body;
	if(flag!=NULL) delete flag;
	if(home!=NULL) delete home;
}
void CFinish::Empty()
{
	EmptyList(list);
	if(top!=NULL) delete top;
	if(body!=NULL) delete body;
	if(flag!=NULL) delete flag;
}
void EmptyList(CList &list)
{
	CNodeBody *p;
	while(list.pHead!=NULL)
	{
		p=list.pHead;
		list.pHead=p->next;
		delete p;
	}
}
bool CFinish::Collision(RECT marioRect)
{
	if(top!=NULL&&top->Collision(marioRect)) 
	{
		flag->isFlagDown=true;	
		return true;
	}
	//if(flag!=NULL&&flag->Collision(marioRect)) return true;
	CNodeBody *p;
	p=list.pHead;
	while(p!=NULL)
	{
		if(p->body->Collision(marioRect)) 
		{
			flag->isFlagDown=true;		
			return true;
		}
		p=p->next;
	}
}
bool CTopFlag::Collision(RECT marioRect)
{
	RECT dest;
	RECT top=topflag->getRECT(x,y);
	if(IntersectRect(&dest,&top,&marioRect)) return true;
	else return false;
}
bool CFlag::Collision(RECT marioRect)
{
	RECT dest;
	RECT flagrect=flag->getRECT(x,y);
	flagrect.left-=20;
	if(IntersectRect(&dest,&flagrect,&marioRect)) 
	{
		isFlagDown=true;	
		
		return true;
	}
	else return false;
}
bool CBodyFlag::Collision(RECT marioRect)
{
	RECT dest;
	RECT bodyflag=body->getRECT(x,y);
	bodyflag.left+=15;
	if(IntersectRect(&dest,&bodyflag,&marioRect)) return true;
	else return false;
}
void CFinish::GetObjects(Object object)
{
	switch(object.kind)
	{
	
	case 32: 
		{
			body=new CBodyFlag(object);
			AddObject(list,body);
		};break;
	case 33: top=new CTopFlag(object);break;
	case 34: flag=new CFlag(object);break;
	}

}
void CFinish::Render(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9 image, int time, int xcamara)
{
	if(top!=NULL) 
		top->Render(sprite,image,time,xcamara);
	if(flag!=NULL) 
		flag->Render(sprite,image,time,xcamara);
	//if(home!=NULL) home->Render(sprite,image,time,xcamara);
	CNodeBody *p;
	p=list.pHead;
	while(p!=NULL)
	{
		p->body->Render(sprite,image,time,xcamara);
		p=p->next;
	}
}
CBodyFlag::CBodyFlag(Object object)
{
	x=object.x;
	y=object.y;
	body=new CSprite(true,64,96,object.width,object.height,0,0,3);
}
CBodyFlag::~CBodyFlag()
{
	if(body!=NULL) delete body;
}
void CBodyFlag::Render(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9 image, int time, int xcamara)
{
	body->Render(sprite,image,x-xcamara,y);
}
CFlag::CFlag(Object object)
{
	x=object.x;
	y=object.y;
	isFlagDown=false;
	flag=new CSprite(true,64,160,object.width,object.height,0,0,5);
}
CFlag::~CFlag()
{
	if(flag!=NULL) delete flag;
}
void CFlag::FallDown()
{
	y+=fall_speed*50;
	if(y>350) y=350;
}
void CFlag::Render(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9 image, int time, int xcamara)
{
	if(isFlagDown==true) FallDown();
	flag->Render(sprite,image,x-xcamara,y);
}
CTopFlag::CTopFlag(Object object)
{
	x=object.x;
	y=object.y;
	topflag=new CSprite(true,105,97,object.width,object.height,0,0,3);
}
CTopFlag::~CTopFlag()
{
	if(topflag!=NULL) delete topflag;
}
void CTopFlag::Render(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9 image, int time, int xcamara)
{
	topflag->Render(sprite,image,x-xcamara,y);
}
void AddObject(CList &list,CBodyFlag *body)
{
	CNodeBody *p=new CNodeBody;
	p->body=body;
	p->next=NULL;
	if(list.pHead==NULL)
	{
		list.pHead=p;
		list.pTail=list.pHead;
	}
	else
	{
		list.pTail->next=p;
		list.pTail=p;
	}
}
CHome::CHome(Object object)
{
	x=object.x;
	y=object.y;
	
}
CHome::~CHome()
{
	if(home!=NULL) delete home;
}

bool CHome::Collision(RECT marioRect)
{
	RECT dest;
	RECT homeRect=home->getRECT(x,y);
	homeRect.left+=64;
	if(IntersectRect(&dest,&marioRect,&homeRect))
	{
		return true;
	}
	else return false;
}